PART 1 Photo of a table was taken. Then using Paint 3D dimensions of the photo were reduced to 512x512 pixel and named as surface. After that, the photo was imported to R Studio.
library(jpeg)
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("EBImage")
## Bioconductor version 3.9 (BiocManager 1.30.9), R 3.6.1 (2019-07-05)
## Installing package(s) 'EBImage'
## package 'EBImage' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'EBImage'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying C:
## \Users\Asus\Documents\R\win-library\3.6\00LOCK\EBImage\libs\x64\EBImage.dll
## to C:\Users\Asus\Documents\R\win-library\3.6\EBImage\libs\x64\EBImage.dll:
## Permission denied
## Warning: restored 'EBImage'
##
## The downloaded binary packages are in
## C:\Users\Asus\AppData\Local\Temp\Rtmp40emzJ\downloaded_packages
## Installation path not writeable, unable to update packages: boot, foreign,
## KernSmooth, mgcv, nlme
library(EBImage)
Question 1
In the first step, jpeg library was downloaded. We defined an array called img and assigned it to surface.jpg to read the photo. We concluded that the size of our array has dimensions 512x512x3 thanks to str(img) command. We used the functions readJPEG and readImage for diferent purposes. We had to use the function readImage for the EBImage library usage.
img <- readJPEG("Surface.jpeg")
image <- readImage("Surface.jpeg")
str(img)
## num [1:512, 1:512, 1:3] 0.867 0.871 0.878 0.878 0.875 ...
Question 2 a)The image was displayed through rasterImage command. b)We downloaded Biocmanager and EBImage packages. We defined a new variable called image. Then we transformed the image into Grayscale by applying colorMode command. Thus, we indicated three seperate channels into one single plot.
plot.new()
rasterImage(img, 0.25, 0, 0.75, 1, interpolate = FALSE)
title(paste("Original Image"))
colorMode(image) <- Grayscale
display(image,method = 'raster', all= TRUE)
Question 3 We defined three different vectors for different channels which are called red, green and blue. We took averages of columns for each channels by apply command. Then, we defined a vector x which contains numbers from 1 to 512. After that, we plotted three lines on the same graph (RGB Average Plot) which shows x values on horizantal axis and calculated averages on vertical axis.
red <- img[0:512,0:512,1 ]
green <- img[0:512,0:512,2 ]
blue <- img[0:512,0:512,3 ]
red_avg <- apply(red, 2, mean)
green_avg <- apply(green, 2, mean)
blue_avg <- apply(blue, 2, mean)
x <- c(1:512)
plot(x,red_avg,type="l",col="red", ylim = c(0.4,0.9) , m = "RGB Average Plot")
lines(x,blue_avg,col="blue")
lines(x,green_avg,col="green")
Question 4 We seperated our channels of the image created at the previous part into two submatrices in equal size vertically. Then for each channels, we subtracted one of the submatrices from the other. Finally, we plotted the new results and displayed them with rasterImage command.The image were mostly black printed which indicated that our image was almost symmetric vertically
# For RED Channel
red1 <- red[0:512,0:256]
red2 <- red[0:512,257:512]
plot.new()
rasterImage(red1,
0.2, 0, 0.45, 1, interpolate = FALSE)
title(paste("Two Parts of RED Channel"))
rasterImage(red2,
0.55, 0, 0.8, 1, interpolate = FALSE)
rednew <- abs(red2-red1)
plot.new()
title(paste("Difference Between Two Parts for RED Channel"))
rasterImage(rednew,
0.25, 0, 0.75, 1, interpolate = FALSE)
#for GREEN CHANNEL
green1 <- green[0:512,0:256]
green2 <- green[0:512,257:512]
plot.new()
rasterImage(green1,
0.2, 0, 0.45, 1, interpolate = FALSE)
title(paste("Two Parts of GREEN Channel"))
rasterImage(green2,
0.55, 0,0.8 , 1, interpolate = FALSE)
greennew <- abs(green2-green1)
plot.new()
title(paste("Difference Between Two Parts for GREEN Channel"))
rasterImage(greennew,
0.25, 0, 0.75, 1, interpolate = FALSE)
#for BLUE CHANNEL
blue1 <- blue[0:512,0:256]
blue2 <- blue[0:512,257:512]
plot.new()
rasterImage(blue1,
0.2, 0, 0.45, 1, interpolate = FALSE)
title(paste("Two Parts of BLUE Channel"))
rasterImage(blue2,
0.55, 0, 0.8, 1, interpolate = FALSE)
bluenew <- abs(blue2-blue1)
plot.new()
rasterImage(bluenew,
0.25, 0, 0.75, 1, interpolate = FALSE)
title(paste("Difference Between Two Parts for BLUE Channel"))
Question 5
Initially,by using medianFilter command we applied median filtering process for 5x5, 11x11, 31x31 dimensions respectively for the image. Thus, we observed resolution differences between the small and big sized filters. While big sized filters caused low quality images small sized filters allowed us to have better qualifed image. In addition, same steps were also followed for different colors as we did in 2b. We applied the median filter with 5x5, 11x11, 31x31 window sizes. The function used in this question takes the size as (the number of rows or columns-1)/2. So we put in 2 for 5x5, 5 for 11x11, and 15 for 31x31.
colorMode(image) <- Color
medFltr5 <- medianFilter(image, 2)
medFltr11 <- medianFilter(image, 5)
medFltr31 <- medianFilter(image, 15)
plot.new()
title(paste("Median Filtered Original Picture 5x5"))
rasterImage(medFltr5 ,
0.25, 0, 0.75, 1, interpolate = FALSE)
plot.new()
title(paste("Median Filtered Original Picture 11x11"))
rasterImage(medFltr11,
0.25, 0, 0.75, 1, interpolate = FALSE)
plot.new()
title(paste("Median Filtered Original Picture 31x31"))
rasterImage(medFltr31,
0.25, 0, 0.75, 1, interpolate = FALSE)
redmd5 <- medianFilter(red, 2)
plot.new()
title(paste("Median Filtered RED Channel 5x5"))
rasterImage(redmd5,0.25,0,0.75,1, interpolate = FALSE)
redmd11 <- medianFilter(red, 5)
plot.new()
title(paste("Median Filtered RED Channel 11x11"))
rasterImage(redmd11,0.25,0,0.75,1, interpolate = FALSE)
redmd31 <- medianFilter(red, 15)
plot.new()
title(paste("Median Filtered RED Channel 31x31"))
rasterImage(redmd31,0.25,0,0.75,1, interpolate = FALSE)
greenmd5 <- medianFilter(green, 2)
plot.new()
title(paste("Median Filtered GREEN Channel 5x5"))
rasterImage(greenmd5,0.25,0,0.75,1, interpolate = FALSE)
greenmd11 <- medianFilter(green, 5)
plot.new()
title(paste("Median Filtered GREEN Channel 11x11"))
rasterImage(greenmd11,0.25,0,0.75,1, interpolate = FALSE)
greenmd31 <- medianFilter(green, 15)
plot.new()
title(paste("Median Filtered GREEN Channel 31x31"))
rasterImage(greenmd31,0.25,0,0.75,1, interpolate = FALSE)
bluemd5 <- medianFilter(blue, 2)
plot.new()
title(paste("Median Filtered BLUE Channel 5x5"))
rasterImage(bluemd5,0.25,0,0.75,1, interpolate = FALSE)
bluemd11 <- medianFilter(blue, 5)
plot.new()
title(paste("Median Filtered BLUE Channel 11x11"))
rasterImage(bluemd11,0.25,0,0.75,1, interpolate = FALSE)
bluemd31 <- medianFilter(blue, 15)
plot.new()
title(paste("Median Filtered BLUE Channel 31x31"))
rasterImage(bluemd31,0.25,0,0.75,1, interpolate = FALSE)
PART 2
1)We transformed our initial image into greyscale format by using Photoshop. We created a new matrix called imggrey and assigned it to the new greyscale photo. Then, we obtained 512x512 dimensioned matrix. We drew a histogram in accordance with the pixel values. We observed that the pixel values were approximately normally distrubuted.
# PART 2
# Question 1
imggrey <- readJPEG("grey.jpg")
plot.new()
title(paste("Edited Gray Image"))
rasterImage(imggrey,0.25,0,0.75,1, interpolate = FALSE)
hist(imggrey[imggrey>0 & imggrey <1], freq = FALSE, main = "Histogram of Pixel Values")
Question 2
We found mean of the pixel values by using mean command. Then by using sd command we calculated the standard deviation and also variance.
# Question 2
meangrey <- mean(imggrey)
std_deviation <- sd(imggrey)
variance <- (std_deviation)^2
print(meangrey)
## [1] 0.5920862
print(std_deviation)
## [1] 0.1059216
print(variance)
## [1] 0.01121938
Question 3 We determined upper and lower bound limits by using qnorm command with mean and standard deviation calculated in the previous part. We also took into account alpha= 0.001 probability for both upper and lower critical values. . The picture vector was defined and assigned to imggrey vector. Then, we set values which are beyond these levels to zero. At the end, we printed the new plot of picture vector and observed some black points on it. We observed black points across the darkest and the lighest parts of the picture. The observed black dots in the picture were more than we expected. This is due to the fact that we assumed there was normal distribution involved and we calculated the mean and the variance according to the this assumption. However the real distribution of the pixel values might differ with a small percentage from the normal distribution.
low_lim <- qnorm(0.001,mean=meangrey,sd=std_deviation)
upper_lim <- qnorm(0.999,mean=meangrey,sd=std_deviation)
picture <- imggrey
picture[picture < low_lim ] <- 0
picture[picture > upper_lim ] <- 0
op <- par()
plot(c(0, 1050), c(0, 512), xlab = "", ylab = "")
title(paste("Fractured Image And The Original"))
rasterImage(picture, 0, 0, 512, 512, interpolate = FALSE)
rasterImage(imggrey, 530, 0, 1042, 512,interpolate = FALSE )
par(op)
## Warning in par(op): grafiksel parametre "cin" belirlenemez
## Warning in par(op): grafiksel parametre "cra" belirlenemez
## Warning in par(op): grafiksel parametre "csi" belirlenemez
## Warning in par(op): grafiksel parametre "cxy" belirlenemez
## Warning in par(op): grafiksel parametre "din" belirlenemez
## Warning in par(op): grafiksel parametre "page" belirlenemez
Question 4
First, we defined two variables i and j set equal to one. Then, we also defined new stdsi, meansi, upper_limi, low_limi. We assigned imggrey to picture2. After that, for every 51x51 matrix square blocks we took i and j into for loop scanning hundred patches.For every loop we calculated, our variables defined at the beginning.We calculated in each step over and under limit values by using our means and standard deviations for each 51x51 patch. Then we extracted these points which are beyond these limits from our original image by equalizing their pixel values to zero in each step. Since we calculated the upper and the lower bounds according to the means and the standard deviations of each of the windows, we observed more black dots than the previous question. This is the result we expected since, the data in each window were less than the whole matrix. Hence, the usage of the patches makes it less likely that this follows normal distribution.
i<- 1
j <- 1
stdsi <- 0
meansi <- 0
low_limi <- 0
upper_limi <- 0
picture2 <- imggrey
for (i in 1:10){
for(j in 1:10){
patch <- picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)]
meansi <- mean(patch)
stdsi <- sd(patch)
low_limi <- qnorm(0.001,mean=meansi,sd=stdsi)
upper_limi <- qnorm(0.999,mean=meansi,sd=stdsi)
picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)] <-ifelse(picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)]<low_limi,0,picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)])
picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)] <- ifelse(picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)]>upper_limi,0,picture2[(51*(i-1)+ 1) : (51*i) , (51*(j-1)+1) : (51*j)])
}
}
op1 <- par()
plot(c(0, 1050), c(0, 512), xlab = "", ylab = "")
title(paste("Fractured Image With 51x51 Window Size And The Original"))
rasterImage(picture2, 0, 0, 512, 512, interpolate = FALSE)
rasterImage(imggrey, 530, 0, 1042, 512,interpolate = FALSE )
par(op1)
## Warning in par(op1): grafiksel parametre "cin" belirlenemez
## Warning in par(op1): grafiksel parametre "cra" belirlenemez
## Warning in par(op1): grafiksel parametre "csi" belirlenemez
## Warning in par(op1): grafiksel parametre "cxy" belirlenemez
## Warning in par(op1): grafiksel parametre "din" belirlenemez
## Warning in par(op1): grafiksel parametre "page" belirlenemez